import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;

import javax.activation.MimetypesFileTypeMap;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.NodeList;
import org.w3c.tidy.Tidy;


public class HtmlParser {
	
	private  HeaderData headerData;
	
	public HeaderData extractInfo(File file) throws IOException {

		if(isHtmFile(file))
		{
			
		headerData = new HeaderData();
			
		Tidy tidy = new Tidy();			
		
		InputStream is = new FileInputStream(file);
		
		tidy.setQuiet(true);
		tidy.setPrintBodyOnly(true);
		tidy.setShowWarnings(false);
		
		Document tidyDOM = tidy.parseDOM(is, null);
		
		//title tag
		/*
		stringTitle = tidyDOM.getElementsByTagName("title").item(0).getFirstChild().getNodeValue();
		System.out.println("Tidy: " + stringTitle);

		//get meta tags
		 * */
		
			try
			{	
			
				NodeList metaTags = tidyDOM.getElementsByTagName("meta");
		 
				for(int i=0; i < metaTags.getLength();i++){			
					//grab the attributes under the tag
					NamedNodeMap attrList = metaTags.item(i).getAttributes();
				
					String s = attrList.getNamedItem("name").getNodeValue();
				
                        
					if(s.compareTo("author") == 0)
					{
						//System.out.println(attrList.getNamedItem().getNodeValue() + "\n"); 
						headerData.SetAuthor(attrList.getNamedItem("content").getNodeValue());
					}
					if(s.compareTo("date") == 0)
					{
						headerData.SetDate(attrList.getNamedItem("content").getNodeValue());
					}
					if(s.compareTo("form") == 0)
					{
						headerData.SetForm(attrList.getNamedItem("content").getNodeValue());
					}
					if(s.compareTo("metadata") == 0)
					{
						headerData.SetMetaData(attrList.getNamedItem("content").getNodeValue());
					}
					if(s.compareTo("objecttype") == 0)
					{
						headerData.SetObjectType(attrList.getNamedItem("content").getNodeValue());
					}
					if(s.compareTo("otheragent") == 0)
					{
						headerData.SetOtherAgent(attrList.getNamedItem("content").getNodeValue());
					}
					if(s.compareTo("publisher") == 0)
					{
						headerData.SetPublisher(attrList.getNamedItem("content").getNodeValue());
					}
					if(s.compareTo("source") == 0)
					{
						headerData.SetSource(attrList.getNamedItem("content").getNodeValue());
					}
					if(s.compareTo("subject") == 0)
					{
						headerData.SetSubject(attrList.getNamedItem("content").getNodeValue());
					}
					if(s.compareTo("title") == 0)
					{
						headerData.SetTitle(attrList.getNamedItem("content").getNodeValue());
					}
					
				}
			}
			catch(NullPointerException n)
			{
				n.printStackTrace();
				
			}
		 }
		
		return headerData;
		
	}
	
	private boolean isHtmFile(File f)
	{
		String filename = f.getName();
		
		if(filename.substring(filename.length() - 4, filename.length()).compareTo(".htm") == 0)
		{
			return true;
		}
		else
		{
			return false;
		}
	}
	

	
}
